package indi.zqc.mqtt.customer.service;


import indi.zqc.mqtt.customer.handler.MqttMessageGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

@Component("wifi")
public class PruducerEcgServiceImpl {


    @Autowired
    private MqttMessageGateway mqttMessageGateway;


    /**
     * 消息处理
     * @return
     */
    @Bean
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public MessageHandler handler() {
        return new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
//                /**  数据解析 */
//                try {
//                    //TODO
//                    DataProto.Content contentData = null;
//                    if (message.getPayload() instanceof byte[]) {
//                        contentData = dataVerification(message.getPayload());
//                    } else {
//                        byte[] data = (byte[]) message.getPayload();
//                        contentData = DataProto.Content.parseFrom(data);
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
                System.out.println("接收时间"+System.currentTimeMillis());
                System.out.println("===>"+ message.getHeaders());


  //                dataVerification(message.getPayload());

                mqttMessageGateway.sendMessage2MqttBytes((byte[]) message.getPayload(),"wootop/wifi/test");
            }

        };
    }
}
